////////////////////////////////////////////////////////////////////////////////////////////////
// English checkers move generator is borrowed from Martin Fierz end game generator software
////////////////////////////////////////////////////////////////////////////////////////////////

#include "Stdafx.h"

#include "Engine.h"

////////////////////////////////////////////////////////////////////////////////////////////////
// English checkers move generator is borrowed from Martin Fierz end game generator software
////////////////////////////////////////////////////////////////////////////////////////////////

// English movegen uses opposite generation model, that white men are at lowest bits
// because of compatibility with endgame db generator
namespace EnglishMoveGen
{
	void MakeWhiteManMoves(BitBoard* bb, BitBoardMove* movelist, int& index, uint32 free)
	{
		int n = index;
		uint32 tmp;
		int m;

		if(bb->wm)
		{
			/* moves left forwards */
			/* I: columns 1357: just moves */
			m=((bb->wm&LF1)<<3)&free;
			/* now m contains a bit for every free square where a black man can move*/
			while(m)
			{
   				tmp=(m&-m); /* least significant bit of m */
				tmp=tmp|(tmp>>3); /* square where man came from */
				movelist[n].wm=tmp&NWBR; /* NWBR: not white back rank */
				movelist[n].wk=tmp&WBR; /*if stone moves to WBR (white back rank) it's a king*/
				movelist[n].bm=0;
				movelist[n].bk=0;
				n++;
      			m=m&(m-1);   /* clears least significant bit of m */
      		}

			 /* II: columns 2468 */
			m=((bb->wm&LF2)<<4)&free;
			while(m)
   			{
				tmp=(m&-m);
				tmp=tmp|(tmp>>4);
				movelist[n].wm=tmp;
				movelist[n].wk=0;
				movelist[n].bm=0;
				movelist[n].bk=0;
				n++;
      			m=m&(m-1);
      		}
			 /* moves right forwards */
			 /* I: columns 1357 :just moves*/
			 m=((bb->wm&RF1)<<4)&free;
			 while(m)
   			{
				tmp=(m&-m);
				tmp=tmp|(tmp>>4);
				movelist[n].wm=tmp&NWBR;
				movelist[n].wk=tmp&WBR;
				movelist[n].bm=0;
				movelist[n].bk=0;
				n++;
      			m=m&(m-1);
      		}

			 /* II: columns 2468 */
			m=((bb->wm&RF2)<<5)&free;
			while(m)
   			{
				tmp=(m&-m);
				tmp=tmp|(tmp>>5);
				movelist[n].wm=tmp;
				movelist[n].wk=0;
				movelist[n].bm=0;
				movelist[n].bk=0;
				n++;
      			m=m&(m-1);
      		}
		} // if

		index = n;
	}

	void MakeBlackManMoves(BitBoard* bb, BitBoardMove* movelist, int& index, uint32 free)
	{
		int n = index;
		uint32 tmp;
		int m;

		if(bb->bm)
		{
			/* moves left backwards */
			/* II: columns 2468 ;just moves*/
			m=((bb->bm&LB2)>>4)&free;
			while(m)
   			{
				tmp=(m&-m);
				tmp=tmp|(tmp<<4);
				movelist[n].wm=0;
				movelist[n].wk=0;
				movelist[n].bm=tmp&NBBR;
				movelist[n].bk=tmp&BBR;
				n++;
      			m=m&(m-1);
      		}
        
			/* I: columns 1357 */
			m=((bb->bm&LB1)>>5)&free;
			/* now m contains a bit for every free square where a white man can move*/
			while(m)
   			{
				tmp=(m&-m); /* least significant bit of m */
				tmp=tmp|(tmp<<5);
				movelist[n].wm=0;
				movelist[n].wk=0;
				movelist[n].bm=tmp;
				movelist[n].bk=0;
      			n++;
      			m=m&(m-1);   /* clears least significant bit of m */
      		}

			/* moves right backwards */

			/* II: columns 2468 : just the moves*/
			m=((bb->bm&RB2)>>3)&free;
			while(m)
   			{
				tmp=(m&-m);
				tmp=tmp|(tmp<<3);
				movelist[n].wm=0;
				movelist[n].wk=0;
				movelist[n].bm=tmp&NBBR;
				movelist[n].bk=tmp&BBR;
				n++;
      			m=m&(m-1);
      		}
			/* I: columns 1357 */
			m=((bb->bm&RB1)>>4)&free;
			while(m)
   			{
				tmp=(m&-m);
				tmp=tmp|(tmp<<4);
				movelist[n].wm=0;
				movelist[n].wk=0;
				movelist[n].bm=tmp;
				movelist[n].bk=0;
      			n++;
      			m=m&(m-1);
      		}
		} // if

		index  = n;
	}

	void MakeWhiteKingMoves(BitBoard* p, BitBoardMove* movelist, int& index, uint32 free)
	{
		int n = index;
		uint32 tmp;
		int m;

		if(p->wk)
      	{
			 /* moves left forwards */
			 /* I: columns 1357 */
			 m=((p->wk&LF1)<<3)&free;
			 /* now m contains a bit for every free square where a black king can move*/
			 while(m)
   			{
				tmp=(m&-m); /* least significant bit of m */
				tmp=tmp|(tmp>>3);
				movelist[n].bm=0;
				movelist[n].wk=tmp;
				movelist[n].wm=0;
				movelist[n].bk=0;
				n++;
      			m=m&(m-1);   /* clears least significant bit of m */
      		}
			 /* II: columns 2468 */
			 m=((p->wk&LF2)<<4)&free;
			 while(m)
   			{
				tmp=(m&-m);
				tmp=tmp|(tmp>>4);
				movelist[n].bm=0;
				movelist[n].wk=tmp;
				movelist[n].wm=0;
				movelist[n].bk=0;
				n++;
      			m=m&(m-1);
      		}
			 /* moves right forwards */
			 /* I: columns 1357 */
			 m=((p->wk&RF1)<<4)&free;
			 while(m)
   			{
				tmp=(m&-m);
				tmp=tmp|(tmp>>4);
				movelist[n].bm=0;
				movelist[n].wk=tmp;
				movelist[n].wm=0;
				movelist[n].bk=0;
				n++;
      			m=m&(m-1);
      		}
			 /* II: columns 2468 */
			 m=((p->wk&RF2)<<5)&free;
			 while(m)
   			{
				tmp=(m&-m);
				tmp=tmp|(tmp>>5);
				movelist[n].bm=0;
				movelist[n].wk=tmp;
				movelist[n].wm=0;
				movelist[n].bk=0;
				n++;
      			m=m&(m-1);
      		}
			 /* moves left backwards */
			 /* I: columns 1357 */
			 m=((p->wk&LB1)>>5)&free;
			 /* now m contains a bit for every free square where a black man can move*/
			 while(m)
   			{
				tmp=(m&-m); /* least significant bit of m */
				tmp=tmp|(tmp<<5);
				movelist[n].bm=0;
				movelist[n].wk=tmp;
				movelist[n].wm=0;
				movelist[n].bk=0;
				n++;
      			m=m&(m-1);   /* clears least significant bit of m */
      		}
			 /* II: columns 2468 */
			 m=((p->wk&LB2)>>4)&free;
			 while(m)
   			{
				tmp=(m&-m);
				tmp=tmp|(tmp<<4);
				movelist[n].bm=0;
				movelist[n].wk=tmp;
				movelist[n].wm=0;
				movelist[n].bk=0;
				n++;
      			m=m&(m-1);
      		}
			 /* moves right backwards */
			 /* I: columns 1357 */
			 m=((p->wk&RB1)>>4)&free;
			 while(m)
   			{
				tmp=(m&-m);
				tmp=tmp|(tmp<<4);
				movelist[n].bm=0;
				movelist[n].wk=tmp;
				movelist[n].wm=0;
				movelist[n].bk=0;
				n++;
      			m=m&(m-1);
      		}
			 /* II: columns 2468 */
			 m=((p->wk&RB2)>>3)&free;
			 while(m)
   			{
				tmp=(m&-m);
				tmp=tmp|(tmp<<3);
				movelist[n].bm=0;
				movelist[n].wk=tmp;
				movelist[n].wm=0;
				movelist[n].bk=0;
				n++;
      			m=m&(m-1);
      		}
         }

		index = n;
	}

	void MakeBlackKingMoves(BitBoard* p, BitBoardMove* movelist, int& index, uint32 free)
	{
		int n = index;
		uint32 tmp;
		int m;

	   	if(p->bk)
      	{
			 /* moves left forwards */
			 /* I: columns 1357 */
			 m=((p->bk&LF1)<<3)&free;
			 /* now m contains a bit for every free square where a black man can move*/
			 while(m)
    		 {
				tmp=(m&-m); /* least significant bit of m */
				tmp=tmp|(tmp>>3);
				movelist[n].bm=0;
				movelist[n].wk=0;
				movelist[n].wm=0;
				movelist[n].bk=tmp;
      			n++;
      			m=m&(m-1);   /* clears least significant bit of m */
      		 }
			 /* II: columns 2468 */
			 m=((p->bk&LF2)<<4)&free;
			 while(m)
   			 {
				tmp=(m&-m);
				tmp=tmp|(tmp>>4);
				movelist[n].bm=0;
				movelist[n].wk=0;
				movelist[n].wm=0;
				movelist[n].bk=tmp;
      			n++;
      			m=m&(m-1);
      		 }
			 /* moves right forwards */
			 /* I: columns 1357 */
			 m=((p->bk&RF1)<<4)&free;
			 while(m)
   				{
				tmp=(m&-m);
				tmp=tmp|(tmp>>4);
				movelist[n].bm=0;
				movelist[n].wk=0;
				movelist[n].wm=0;
				movelist[n].bk=tmp;
      			n++;
      			m=m&(m-1);
      		 }
			 /* II: columns 2468 */
			 m=((p->bk&RF2)<<5)&free;
			 while(m)
   			 {
				tmp=(m&-m);
				tmp=tmp|(tmp>>5);
				movelist[n].bm=0;
				movelist[n].wk=0;
				movelist[n].wm=0;
				movelist[n].bk=tmp;
      			n++;
      			m=m&(m-1);
      		 }
			 /* moves left backwards */
			 /* I: columns 1357 */
			 m=((p->bk&LB1)>>5)&free;
			 /* now m contains a bit for every free square where a black man can move*/
			 while(m)
   			 {
				tmp=(m&-m); /* least significant bit of m */
				tmp=tmp|(tmp<<5);
				movelist[n].bm=0;
				movelist[n].wk=0;
				movelist[n].wm=0;
				movelist[n].bk=tmp;
      			n++;
      			m=m&(m-1);   /* clears least significant bit of m */
      		 }
			 /* II: columns 2468 */
			 m=((p->bk&LB2)>>4)&free;
			 while(m)
   			 {
				tmp=(m&-m);
				tmp=tmp|(tmp<<4);
				movelist[n].bm=0;
				movelist[n].wk=0;
				movelist[n].wm=0;
				movelist[n].bk=tmp;
      			n++;
      			m=m&(m-1);
      		 }
			 /* moves right backwards */
			 /* I: columns 1357 */
			 m=((p->bk&RB1)>>4)&free;
			 while(m)
   			 {
				tmp=(m&-m);
				tmp=tmp|(tmp<<4);
				movelist[n].bm=0;
				movelist[n].wk=0;
				movelist[n].wm=0;
				movelist[n].bk=tmp;
      			n++;
      			m=m&(m-1);
      		 }
			 /* II: columns 2468 */
			 m=((p->bk&RB2)>>3)&free;
			 while(m)
   			 {
				tmp=(m&-m);
				tmp=tmp|(tmp<<3);
				movelist[n].bm=0;
				movelist[n].wk=0;
				movelist[n].wm=0;
				movelist[n].bk=tmp;
      			n++;
      			m=m&(m-1);
      		 }
         }

		index = n;
	}


	int MakeMoveList(BitBoard *bb, BitBoardMove* movelist, bool bWhite)
	{
		int index = 0;
		uint32 free=~(bb->bm|bb->bk|bb->wm|bb->wk);
   
		if(bWhite == true)
		{
			MakeWhiteKingMoves(bb, movelist, index, free);
			MakeWhiteManMoves(bb, movelist, index, free);     
		}
		else
		{
			MakeBlackKingMoves(bb, movelist, index, free);
			MakeBlackManMoves(bb, movelist, index, free);
		}

		return index;
	}

	 // capture list 
	/////////////////////////////////////////////////////////////////////////////////////////////////////


	void WhiteManCapture1(BitBoard *p, BitBoardMove* movelist,int *n, BitBoardMove *partial, uint32 square)
	{
		/* partial move has already been executed. seek LFJ1 and RFJ1 */
		uint32 m,free,black;
		bool found=false;
		BitBoardMove next_partial,whole_partial;
		BitBoard q;

		free=~(p->bm|p->bk|p->wm|p->wk);
		black=p->bm|p->bk;
	   /* left forward jump */
		m=((((square&LFJ1)<<3)&black)<<4)&free;
		if(m)
   		{
			next_partial.wm=(m|(m>>7));
			next_partial.wk=0;
			next_partial.bm=(m>>4)&p->bm;
			next_partial.bk=(m>>4)&p->bk;
			q.bm=p->bm^next_partial.bm;
			q.bk=p->bk^next_partial.bk;
   			q.wm=p->wm^next_partial.wm;
   			q.wk=p->wk^next_partial.wk;
			whole_partial.bm=partial->bm^next_partial.bm;
			whole_partial.bk=partial->bk^next_partial.bk;
			whole_partial.wm=partial->wm^next_partial.wm;
			whole_partial.wk=partial->wk^next_partial.wk;
			WhiteManCapture1(&q,movelist,n,&whole_partial,m);
     
			found=true;
		 } // if

		/* right forward jump */
		m=((((square&RFJ1)<<4)&black)<<5)&free;
		if(m)
		{
			next_partial.wm=(m|(m>>9));
			next_partial.wk=0;
			next_partial.bm=(m>>5)&p->bm;
			next_partial.bk=(m>>5)&p->bk;
			q.bm=p->bm^next_partial.bm;
			q.bk=p->bk^next_partial.bk;
   			q.wm=p->wm^next_partial.wm;
   			q.wk=p->wk^next_partial.wk;
      
			whole_partial.bm=partial->bm^next_partial.bm;
			whole_partial.bk=partial->bk^next_partial.bk;
			whole_partial.wm=partial->wm^next_partial.wm;
			whole_partial.wk=partial->wk^next_partial.wk;
			WhiteManCapture1(&q,movelist,n,&whole_partial,m);
      
			found=true;
		}

		if(!found)
   		{
			movelist[*n]=*partial;
			(*n)++;
		}
	}

	void WhiteManCapture2(BitBoard *p, BitBoardMove* movelist,int *n, BitBoardMove *partial, uint32 square)
	{
		uint32 m,free,black;
		BitBoardMove next_partial,whole_partial;
		bool found=false;
		BitBoard q;

		free=~(p->bm|p->bk|p->wm|p->wk);
		black=p->bm|p->bk;
	   /* left forward jump */
		m=((((square&LFJ2)<<4)&black)<<3)&free;
		if(m)
   		{
			next_partial.wm=(m|(m>>7))&NWBR;
			next_partial.wk=(m|(m>>7))&WBR;
			next_partial.bm=(m>>3)&p->bm;
			next_partial.bk=(m>>3)&p->bk;
			q.bm=p->bm^next_partial.bm;
			q.bk=p->bk^next_partial.bk;
   			q.wm=p->wm^next_partial.wm;
   			q.wk=p->wk^next_partial.wk;
      
			whole_partial.bm=partial->bm^next_partial.bm;
			whole_partial.bk=partial->bk^next_partial.bk;
			whole_partial.wm=partial->wm^next_partial.wm;
			whole_partial.wk=partial->wk^next_partial.wk;
			WhiteManCapture2(&q,movelist,n,&whole_partial,m);
      
			found=true;
		}

	   /* right forward jump */
		m=((((square&RFJ2)<<5)&black)<<4)&free;
		if(m)
   		{
			next_partial.wm=(m|(m>>9))&NWBR;
			next_partial.wk=(m|(m>>9))&WBR;
			next_partial.bm=(m>>4)&p->bm;
			next_partial.bk=(m>>4)&p->bk;
			q.bm=p->bm^next_partial.bm;
			q.bk=p->bk^next_partial.bk;
   			q.wm=p->wm^next_partial.wm;
   			q.wk=p->wk^next_partial.wk;
      
			whole_partial.bm=partial->bm^next_partial.bm;
			whole_partial.bk=partial->bk^next_partial.bk;
			whole_partial.wm=partial->wm^next_partial.wm;
			whole_partial.wk=partial->wk^next_partial.wk;
			WhiteManCapture2(&q,movelist,n,&whole_partial,m);
      
			found=true;
		}

		if(!found)
   		{
			movelist[*n]=*partial;
			(*n)++;
		}
	}


	void BlackManCapture1(BitBoard *p, BitBoardMove* movelist,int *n, BitBoardMove* partial, uint32 square)
	{
		uint32 m,free,white;
		BitBoardMove next_partial,whole_partial;
		int found=0;
		BitBoard q;

		free=~(p->bm|p->bk|p->wm|p->wk);
		white=p->wm|p->wk;
		/* left backward jump */
		m=((((square&LBJ1)>>5)&white)>>4)&free;
		if(m)
   		{
			next_partial.bm=(m|(m<<9))&NBBR;
			next_partial.bk=(m|(m<<9))&BBR;
			next_partial.wm=(m<<4)&p->wm;
			next_partial.wk=(m<<4)&p->wk;
			q.bm=p->bm^next_partial.bm;
			q.bk=p->bk^next_partial.bk;
   			q.wm=p->wm^next_partial.wm;
   			q.wk=p->wk^next_partial.wk;
      
			whole_partial.bm=partial->bm^next_partial.bm;
			whole_partial.bk=partial->bk^next_partial.bk;
			whole_partial.wm=partial->wm^next_partial.wm;
			whole_partial.wk=partial->wk^next_partial.wk;
			BlackManCapture1(&q,movelist,n,&whole_partial,m);
      
			found=1;
		}

	   /* right backward jump */
		m=((((square&RBJ1)>>4)&white)>>3)&free;
		if(m)
   		{
			next_partial.bm=(m|(m<<7))&NBBR;
			next_partial.bk=(m|(m<<7))&BBR;
			next_partial.wm=(m<<3)&p->wm;
			next_partial.wk=(m<<3)&p->wk;
			q.bm=p->bm^next_partial.bm;
			q.bk=p->bk^next_partial.bk;
   			q.wm=p->wm^next_partial.wm;
   			q.wk=p->wk^next_partial.wk;
      
			whole_partial.bm=partial->bm^next_partial.bm;
			whole_partial.bk=partial->bk^next_partial.bk;
			whole_partial.wm=partial->wm^next_partial.wm;
			whole_partial.wk=partial->wk^next_partial.wk;
			BlackManCapture1(&q,movelist,n,&whole_partial,m);
      
			found=1;
		}

		if(!found)
   		{
			movelist[*n]=*partial;
			(*n)++;
		}
	}

	void BlackManCapture2(BitBoard *p, BitBoardMove* movelist,int *n, BitBoardMove *partial, uint32 square)
	{
		uint32 m,free,white;
		BitBoardMove next_partial,whole_partial;
		int found=0;
		BitBoard q;
	
		free=~(p->bm|p->bk|p->wm|p->wk);
		white=p->wm|p->wk;
	   /* left backward jump */
		m=((((square&LBJ2)>>4)&white)>>5)&free;
		if(m)
   		{
			next_partial.bm=(m|(m<<9));
			next_partial.bk=0;
			next_partial.wm=(m<<5)&p->wm;
			next_partial.wk=(m<<5)&p->wk;
			q.bm=p->bm^next_partial.bm;
			q.bk=p->bk^next_partial.bk;
   			q.wm=p->wm^next_partial.wm;
   			q.wk=p->wk^next_partial.wk;
      
			whole_partial.bm=partial->bm^next_partial.bm;
			whole_partial.bk=partial->bk^next_partial.bk;
			whole_partial.wm=partial->wm^next_partial.wm;
			whole_partial.wk=partial->wk^next_partial.wk;
			BlackManCapture2(&q,movelist,n,&whole_partial,m);
      
			found=1;
		}

	   /* right backward jump */
		m=((((square&RBJ2)>>3)&white)>>4)&free;
		if(m)
   		{
			next_partial.bm=(m|(m<<7));
			next_partial.bk=0;
			next_partial.wm=(m<<4)&p->wm;
			next_partial.wk=(m<<4)&p->wk;
			q.bm=p->bm^next_partial.bm;
			q.bk=p->bk^next_partial.bk;
   			q.wm=p->wm^next_partial.wm;
   			q.wk=p->wk^next_partial.wk;
      
			whole_partial.bm=partial->bm^next_partial.bm;
			whole_partial.bk=partial->bk^next_partial.bk;
			whole_partial.wm=partial->wm^next_partial.wm;
			whole_partial.wk=partial->wk^next_partial.wk;
			BlackManCapture2(&q,movelist,n,&whole_partial,m);
      
			found=1;
		}

		if(!found)
		{
			movelist[*n]=*partial;
			(*n)++;
		}
	}


	void MakeWhiteManCaptures(BitBoard* p, BitBoardMove* movelist, int& n)
	{
		int m;
		uint32 tmp;
		BitBoardMove partial;
		uint32 black = 0, black2, free2;
		uint32 free = ~(p->bm|p->bk|p->wm|p->wk);
		BitBoard q;

		if(p->wm)
		{
			black=p->bm|p->bk;
      		m=((((p->wm&LFJ2)<<4)&black)<<3)&free;
			while(m)
   			{
				/* find a move */
				tmp=(m&-m); /* least significant bit of m */
				partial.wm=(tmp|(tmp>>7))&NWBR;  /* NWBR: not white back rank */
				partial.wk=(tmp|(tmp>>7))&WBR;  /*if stone moves to WBR (white back rank) it's a king*/
				partial.bm=(tmp>>3)&p->bm;
				partial.bk=(tmp>>3)&p->bk;
				/* toggle it */
				q.bm=p->bm^partial.bm;
   				q.bk=p->bk^partial.bk;
   				q.wm=p->wm^partial.wm;
   				q.wk=p->wk^partial.wk;
				/* recursion */
				/* only if black has another capture move! */
				black2=p->bm|p->bk;
				free2=~(p->wm|p->wk|p->bm|p->bk);
				if ( (((((tmp&LFJ2)<<4)&black2)<<3)&free2) | (((((tmp&RFJ2)<<5)&black2)<<4)&free2))
            		WhiteManCapture2(&q,movelist, &n, &partial,tmp);
				else
				{
      				movelist[n]=partial;
      				n++;
				}
            
				/* clears least significant bit of m, associated with that move. */
      			m=m&(m-1);
  			}

			m=((((p->wm&LFJ1)<<3)&black)<<4)&free;
      		/* now m contains a bit for every free square where a black man can move*/
			while(m)
   			{
				tmp=(m&-m); /* least significant bit of m */
				partial.wm=(tmp|(tmp>>7));
				partial.wk=0;
				partial.bm=(tmp>>4)&p->bm;
				partial.bk=(tmp>>4)&p->bk;
				q.bm=p->bm^partial.bm;
   				q.bk=p->bk^partial.bk;
   				q.wm=p->wm^partial.wm;
   				q.wk=p->wk^partial.wk;
            

				black2=p->bm|p->bk;
				free2=~(p->wm|p->wk|p->bm|p->bk);
				if ( ( ((((tmp&LFJ1)<<3)&black2)<<4)&free2 ) | ( ((((tmp&RFJ1)<<4)&black2)<<5)&free2 ))
            		WhiteManCapture1(&q,movelist,&n,&partial,tmp);
				else
				{
      				movelist[n]=partial;
      				n++;
				}
            
      			m=m&(m-1);   /* clears least significant bit of m */
			}

			/* jumps right forwards with men*/
			m=((((p->wm&RFJ2)<<5)&black)<<4)&free;
      		/* now m contains a bit for every free square where a black man can move*/
			while(m)
   			{
				tmp=(m&-m); /* least significant bit of m */
				partial.wm=(tmp|(tmp>>9))&NWBR;
				partial.wk=(tmp|(tmp>>9))&WBR;
				partial.bm=(tmp>>4)&p->bm;
				partial.bk=(tmp>>4)&p->bk;
				q.bm=p->bm^partial.bm;
   				q.bk=p->bk^partial.bk;
   				q.wm=p->wm^partial.wm;
   				q.wk=p->wk^partial.wk;
            
				black2=p->bm|p->bk;
				free2=~(p->wm|p->wk|p->bm|p->bk);
				if ( ( ((((tmp&LFJ2)<<4)&black2)<<3)&free2 ) | ( ((((tmp&RFJ2)<<5)&black2)<<4)&free2 ))
            		WhiteManCapture2(&q,movelist,&n, &partial,tmp);
				else
				{
      				movelist[n]=partial;
      				n++;
				}
            
      			m=m&(m-1);   /* clears least significant bit of m */
			}

			m=((((p->wm&RFJ1)<<4)&black)<<5)&free;
      		/* now m contains a bit for every free square where a black man can move*/
			while(m)
   			{
				tmp=(m&-m); /* least significant bit of m */
				partial.wm=tmp|(tmp>>9);
				partial.wk=0;
				partial.bm=(tmp>>5)&p->bm;
				partial.bk=(tmp>>5)&p->bk;
				q.bm=p->bm^partial.bm;
   				q.bk=p->bk^partial.bk;
   				q.wm=p->wm^partial.wm;
   				q.wk=p->wk^partial.wk;
            
				black2=p->bm|p->bk;
				free2=~(p->wm|p->wk|p->bm|p->bk);
				if ( ( ((((tmp&LFJ1)<<3)&black2)<<4)&free2 ) | ( ((((tmp&RFJ1)<<4)&black2)<<5)&free2 ))
            		WhiteManCapture1(&q,movelist, &n, &partial,tmp);
				else
				{
      				movelist[n]=partial;
      				n++;
				}
            
      			m=m&(m-1);   /* clears least significant bit of m */
			}
		} // if
	}

	void MakeBlackManCaptures(BitBoard* p, BitBoardMove* movelist, int& n)
	{
		int m;
		uint32 tmp;
		BitBoardMove partial;
		uint32 white = 0, white2, free2;
		uint32 free = ~(p->bm|p->bk|p->wm|p->wk);
		BitBoard q;

		if(p->bm)
		{
			white=p->wm|p->wk;
      		/* jumps left backwards with men*/
      		m=((((p->bm&LBJ1)>>5)&white)>>4)&free;
      		/* now m contains a bit for every free square where a white man can move*/
			while(m)
   			{
				tmp=(m&-m); /* least significant bit of m */
				partial.bm=(tmp|(tmp<<9))&NBBR;
				partial.bk=(tmp|(tmp<<9))&BBR;
				partial.wm=(tmp<<4)&p->wm;
				partial.wk=(tmp<<4)&p->wk;
				q.bm=p->bm^partial.bm;
   				q.bk=p->bk^partial.bk;
   				q.wm=p->wm^partial.wm;
   				q.wk=p->wk^partial.wk;
				/* only if white has another capture move! */
				white2=p->wm|p->wk;
				free2=~(p->wm|p->wk|p->bm|p->bk);
				if ( ( ((((tmp&LBJ1)>>5)&white2)>>4)&free2) | ( ((((tmp&RBJ1)>>4)&white2)>>3)&free2 ))
            		BlackManCapture1(&q,movelist, &n, &partial,tmp);
				else
				{
      				movelist[n]=partial;
      				n++;
				}
            
				m=m&(m-1);   /* clears least significant bit of m */
			}

			m=((((p->bm&LBJ2)>>4)&white)>>5)&free;
      		/* now m contains a bit for every free square where a white man can move*/
			while(m)
   			{
				tmp=(m&-m); /* least significant bit of m */
				partial.bm=(tmp|(tmp<<9));
				partial.bk=0;
				partial.wm=(tmp<<5)&p->wm;
				partial.wk=(tmp<<5)&p->wk;
				q.bm=p->bm^partial.bm;
   				q.bk=p->bk^partial.bk;
   				q.wm=p->wm^partial.wm;
   				q.wk=p->wk^partial.wk;

				white2=p->wm|p->wk;
				free2=~(p->wm|p->wk|p->bm|p->bk);
				if ( ( ((((tmp&LBJ2)>>4)&white2)>>5)&free2) | ( ((((tmp&RBJ2)>>3)&white2)>>4)&free2 ))
            		BlackManCapture2(&q,movelist,&n, &partial,tmp);
				else
				{
      				movelist[n]=partial;
      				n++;
				}
            
      			m=m&(m-1);   /* clears least significant bit of m */
			}

			/* jumps right backwards with men*/
      		m=((((p->bm&RBJ1)>>4)&white)>>3)&free;
      		/* now m contains a bit for every free square where a white man can move*/
			while(m)
   			{
				tmp=(m&-m); /* least significant bit of m */
				partial.bm=(tmp|(tmp<<7))&NBBR;
				partial.bk=(tmp|(tmp<<7))&BBR;
				partial.wm=(tmp<<3)&p->wm;
				partial.wk=(tmp<<3)&p->wk;
				q.bm=p->bm^partial.bm;
   				q.bk=p->bk^partial.bk;
   				q.wm=p->wm^partial.wm;
   				q.wk=p->wk^partial.wk;

				white2=p->wm|p->wk;
				free2=~(p->wm|p->wk|p->bm|p->bk);
				if ( ( ((((tmp&LBJ1)>>5)&white2)>>4)&free2) | ( ((((tmp&RBJ1)>>4)&white2)>>3)&free2 ))
            		BlackManCapture1(&q,movelist, &n, &partial,tmp);
				else
				{
      				movelist[n]=partial;
      				n++;
				}
            
      			m=m&(m-1);   /* clears least significant bit of m */
			}

			m=((((p->bm&RBJ2)>>3)&white)>>4)&free;
      		/* now m contains a bit for every free square where a black man can move*/
			while(m)
   			{
				tmp=(m&-m); /* least significant bit of m */
				partial.bm=(tmp|(tmp<<7));
				partial.bk=0;
				partial.wm=(tmp<<4)&p->wm;
				partial.wk=(tmp<<4)&p->wk;
				q.bm=p->bm^partial.bm;
   				q.bk=p->bk^partial.bk;
   				q.wm=p->wm^partial.wm;
   				q.wk=p->wk^partial.wk;
            
				white2=p->wm|p->wk;
				free2=~(p->wm|p->wk|p->bm|p->bk);
				if ( ( ((((tmp&LBJ2)>>4)&white2)>>5)&free2) | ( ((((tmp&RBJ2)>>3)&white2)>>4)&free2 ))
            		BlackManCapture2(&q,movelist, &n, &partial,tmp);
				else
				{
      				movelist[n]=partial;
      				n++;
				}
            
      			m=m&(m-1);   /* clears least significant bit of m */
			}
		}
	}

	void WhiteKingCapture1(BitBoard *p, BitBoardMove* movelist,int *n, BitBoardMove *partial, uint32 square)
	{
	   /* partial move has already been executed. seek LFJ1 RFJ1 LBJ1 RBJ1*/
	   uint32 m,free,black;
	   BitBoardMove next_partial, whole_partial;
	   int found=0;
	   BitBoard q;

	   free=~(p->bm|p->bk|p->wm|p->wk);
	   black=p->bm|p->bk;
	   /* left forward jump */
	   m=((((square&LFJ1)<<3)&black)<<4)&free;
	   if(m)
   	   {
		  next_partial.wm=0;
		  next_partial.wk=(m|(m>>7));
		  next_partial.bm=(m>>4)&p->bm;
		  next_partial.bk=(m>>4)&p->bk;
		  q.bm=p->bm^next_partial.bm;
		  q.bk=p->bk^next_partial.bk;
   		  q.wm=p->wm^next_partial.wm;
   		  q.wk=p->wk^next_partial.wk;
      
		  whole_partial.bm=partial->bm^next_partial.bm;
		  whole_partial.bk=partial->bk^next_partial.bk;
		  whole_partial.wm=partial->wm^next_partial.wm;
		  whole_partial.wk=partial->wk^next_partial.wk;
		  WhiteKingCapture1(&q,movelist,n,&whole_partial,m);
      
		  found=1;
	   }

	   /* right forward jump */
	   m=((((square&RFJ1)<<4)&black)<<5)&free;
	   if(m)
   	   {
		  next_partial.wm=0;
		  next_partial.wk=(m|(m>>9));
		  next_partial.bm=(m>>5)&p->bm;
		  next_partial.bk=(m>>5)&p->bk;
		  q.bm=p->bm^next_partial.bm;
		  q.bk=p->bk^next_partial.bk;
   		  q.wm=p->wm^next_partial.wm;
   		  q.wk=p->wk^next_partial.wk;
      
		  whole_partial.bm=partial->bm^next_partial.bm;
		  whole_partial.bk=partial->bk^next_partial.bk;
		  whole_partial.wm=partial->wm^next_partial.wm;
		  whole_partial.wk=partial->wk^next_partial.wk;
		  WhiteKingCapture1(&q,movelist,n,&whole_partial,m);
      
		  found=1;
	   }

	   /* left backward jump */
	   m=((((square&LBJ1)>>5)&black)>>4)&free;
	   if(m)
   	   {
		  next_partial.wm=0;
		  next_partial.wk=(m|(m<<9));
		  next_partial.bm=(m<<4)&p->bm;
		  next_partial.bk=(m<<4)&p->bk;
		  q.bm=p->bm^next_partial.bm;
		  q.bk=p->bk^next_partial.bk;
   		  q.wm=p->wm^next_partial.wm;
   		  q.wk=p->wk^next_partial.wk;
      
		  whole_partial.bm=partial->bm^next_partial.bm;
		  whole_partial.bk=partial->bk^next_partial.bk;
		  whole_partial.wm=partial->wm^next_partial.wm;
		  whole_partial.wk=partial->wk^next_partial.wk;
		  WhiteKingCapture1(&q,movelist,n,&whole_partial,m);
      
		  found=1;
	   }

	   /* right backward jump */
	   m=((((square&RBJ1)>>4)&black)>>3)&free;
	   if(m)
   	   {
		  next_partial.wm=0;
		  next_partial.wk=(m|(m<<7));
		  next_partial.bm=(m<<3)&p->bm;
		  next_partial.bk=(m<<3)&p->bk;
		  q.bm=p->bm^next_partial.bm;
		  q.bk=p->bk^next_partial.bk;
   		  q.wm=p->wm^next_partial.wm;
   		  q.wk=p->wk^next_partial.wk;
      
		  whole_partial.bm=partial->bm^next_partial.bm;
		  whole_partial.bk=partial->bk^next_partial.bk;
		  whole_partial.wm=partial->wm^next_partial.wm;
		  whole_partial.wk=partial->wk^next_partial.wk;
		  WhiteKingCapture1(&q,movelist,n,&whole_partial,m);
      
		  found=1;
	   }

	   if(!found)
   	   {
		  /* no continuing jumps - save the move in the movelist */          
		  movelist[*n]=*partial;
		  (*n)++;
		}
   }


	void WhiteKingCapture2(BitBoard *p, BitBoardMove* movelist,int *n, BitBoardMove *partial, uint32 square)
	{
	   /* partial move has already been executed. seek LFJ1 RFJ1 LBJ1 RBJ1*/
	   uint32 m,free,black;
	   BitBoardMove next_partial,whole_partial;
	   int found=0;
	   BitBoard q;

	   free=~(p->bm|p->bk|p->wm|p->wk);
	   black=p->bm|p->bk;
	   /* left forward jump */
	   m=((((square&LFJ2)<<4)&black)<<3)&free;
	   if(m)
   	   {
		  next_partial.wm=0;
		  next_partial.wk=(m|(m>>7));
		  next_partial.bm=(m>>3)&p->bm;
		  next_partial.bk=(m>>3)&p->bk;
		  q.bm=p->bm^next_partial.bm;
		  q.bk=p->bk^next_partial.bk;
   		  q.wm=p->wm^next_partial.wm;
   		  q.wk=p->wk^next_partial.wk;
      
		  whole_partial.bm=partial->bm^next_partial.bm;
		  whole_partial.bk=partial->bk^next_partial.bk;
		  whole_partial.wm=partial->wm^next_partial.wm;
		  whole_partial.wk=partial->wk^next_partial.wk;
		  WhiteKingCapture2(&q,movelist,n,&whole_partial,m);
      
		  found=1;
	   }

	   /* right forward jump */
	   m=((((square&RFJ2)<<5)&black)<<4)&free;
	   if(m)
   	   {
		  next_partial.wm=0;
		  next_partial.wk=(m|(m>>9));
		  next_partial.bm=(m>>4)&p->bm;
		  next_partial.bk=(m>>4)&p->bk;
		  q.bm=p->bm^next_partial.bm;
		  q.bk=p->bk^next_partial.bk;
   		  q.wm=p->wm^next_partial.wm;
   		  q.wk=p->wk^next_partial.wk;
      
		  whole_partial.bm=partial->bm^next_partial.bm;
		  whole_partial.bk=partial->bk^next_partial.bk;
		  whole_partial.wm=partial->wm^next_partial.wm;
		  whole_partial.wk=partial->wk^next_partial.wk;
		  WhiteKingCapture2(&q,movelist,n,&whole_partial,m);
      
		  found=1;
	   }

	   /* left backward jump */
	   m=((((square&LBJ2)>>4)&black)>>5)&free;
	   if(m)
   	   {
		  next_partial.wm=0;
		  next_partial.wk=(m|(m<<9));
		  next_partial.bm=(m<<5)&p->bm;
		  next_partial.bk=(m<<5)&p->bk;
		  q.bm=p->bm^next_partial.bm;
		  q.bk=p->bk^next_partial.bk;
   		  q.wm=p->wm^next_partial.wm;
   		  q.wk=p->wk^next_partial.wk;
      
		  whole_partial.bm=partial->bm^next_partial.bm;
		  whole_partial.bk=partial->bk^next_partial.bk;
		  whole_partial.wm=partial->wm^next_partial.wm;
		  whole_partial.wk=partial->wk^next_partial.wk;
		  WhiteKingCapture2(&q,movelist,n,&whole_partial,m);
      
		  found=1;
	   }

	   /* right backward jump */

	   m=((((square&RBJ2)>>3)&black)>>4)&free;
	   if(m)
   	   {
		  next_partial.wm=0;
		  next_partial.wk=(m|(m<<7));
		  next_partial.bm=(m<<4)&p->bm;
		  next_partial.bk=(m<<4)&p->bk;
		  q.bm=p->bm^next_partial.bm;
		  q.bk=p->bk^next_partial.bk;
   		  q.wm=p->wm^next_partial.wm;
   		  q.wk=p->wk^next_partial.wk;
      
		  whole_partial.bm=partial->bm^next_partial.bm;
		  whole_partial.bk=partial->bk^next_partial.bk;
		  whole_partial.wm=partial->wm^next_partial.wm;
		  whole_partial.wk=partial->wk^next_partial.wk;
		  WhiteKingCapture2(&q,movelist,n,&whole_partial,m);
      
		  found=1;
	   }

	   if(!found)
   	   {
		  /* no continuing jumps - save the move in the movelist */          
		  movelist[*n]=*partial;
		  (*n)++;
	   }
   }

    void BlackKingCapture1(BitBoard *p, BitBoardMove* movelist,int *n, BitBoardMove *partial, uint32 square)
	{
	   /* partial move has already been executed. seek LFJ1 RFJ1 LBJ1 RBJ1*/
	   uint32 m,free,white;
	   BitBoardMove next_partial,whole_partial;
	   int found=0;
	   BitBoard q;

	   free=~(p->bm|p->bk|p->wm|p->wk);
	   white=p->wm|p->wk;
	   /* left forward jump */
	   m=((((square&LFJ1)<<3)&white)<<4)&free;
	   if(m)
   	   {
		  next_partial.bm=0;
		  next_partial.bk=(m|(m>>7));
		  next_partial.wm=(m>>4)&p->wm;
		  next_partial.wk=(m>>4)&p->wk;
		  q.bm=p->bm^next_partial.bm;
		  q.bk=p->bk^next_partial.bk;
   		  q.wm=p->wm^next_partial.wm;
   		  q.wk=p->wk^next_partial.wk;
      
		  whole_partial.bm=partial->bm^next_partial.bm;
		  whole_partial.bk=partial->bk^next_partial.bk;
		  whole_partial.wm=partial->wm^next_partial.wm;
		  whole_partial.wk=partial->wk^next_partial.wk;
		  BlackKingCapture1(&q,movelist,n,&whole_partial,m);
      
		  found=1;
	   }

	   /* right forward jump */
	   m=((((square&RFJ1)<<4)&white)<<5)&free;
	   if(m)
   	   {
		  next_partial.bm=0;
		  next_partial.bk=(m|(m>>9));
		  next_partial.wm=(m>>5)&p->wm;
		  next_partial.wk=(m>>5)&p->wk;
		  q.bm=p->bm^next_partial.bm;
		  q.bk=p->bk^next_partial.bk;
   		  q.wm=p->wm^next_partial.wm;
   		  q.wk=p->wk^next_partial.wk;
      
		  whole_partial.bm=partial->bm^next_partial.bm;
		  whole_partial.bk=partial->bk^next_partial.bk;
		  whole_partial.wm=partial->wm^next_partial.wm;
		  whole_partial.wk=partial->wk^next_partial.wk;
		  BlackKingCapture1(&q,movelist,n,&whole_partial,m);
      
		  found=1;
		}

	   /* left backward jump */
	   m=((((square&LBJ1)>>5)&white)>>4)&free;
	   if(m)
   	   {
		  next_partial.bm=0;
		  next_partial.bk=(m|(m<<9));
		  next_partial.wm=(m<<4)&p->wm;
		  next_partial.wk=(m<<4)&p->wk;
		  q.bm=p->bm^next_partial.bm;
		  q.bk=p->bk^next_partial.bk;
   		  q.wm=p->wm^next_partial.wm;
   		  q.wk=p->wk^next_partial.wk;
      
		  whole_partial.bm=partial->bm^next_partial.bm;
		  whole_partial.bk=partial->bk^next_partial.bk;
		  whole_partial.wm=partial->wm^next_partial.wm;
		  whole_partial.wk=partial->wk^next_partial.wk;
		  BlackKingCapture1(&q,movelist,n,&whole_partial,m);
      
		  found=1;
		}

	   /* right backward jump */
	   m=((((square&RBJ1)>>4)&white)>>3)&free;
	   if(m)
   	   {
		  next_partial.bm=0;
		  next_partial.bk=(m|(m<<7));
		  next_partial.wm=(m<<3)&p->wm;
		  next_partial.wk=(m<<3)&p->wk;
		  q.bm=p->bm^next_partial.bm;
		  q.bk=p->bk^next_partial.bk;
   		  q.wm=p->wm^next_partial.wm;
   		  q.wk=p->wk^next_partial.wk;
      
		  whole_partial.bm=partial->bm^next_partial.bm;
		  whole_partial.bk=partial->bk^next_partial.bk;
		  whole_partial.wm=partial->wm^next_partial.wm;
		  whole_partial.wk=partial->wk^next_partial.wk;
		  BlackKingCapture1(&q,movelist,n,&whole_partial,m);
      
		  found=1;
	   }

	   if(!found)
   	   {
		  /* no continuing jumps - save the move in the movelist */         
		  movelist[*n]=*partial;
		  (*n)++;
	    }
   }

	void BlackKingCapture2(BitBoard *p, BitBoardMove* movelist,int *n, BitBoardMove *partial, uint32 square)
	{
	   /* partial move has already been executed. seek LFJ1 RFJ1 LBJ1 RBJ1*/
	   uint32 m,free,white;
	   BitBoardMove next_partial,whole_partial;
	   int found=0;
	   BitBoard q;

	   free=~(p->bm|p->bk|p->wm|p->wk);
	   white=p->wm|p->wk;
	   /* left forward jump */
	   m=((((square&LFJ2)<<4)&white)<<3)&free;
	   if(m)
   	   {
		  next_partial.bm=0;
		  next_partial.bk=(m|(m>>7));
		  next_partial.wm=(m>>3)&p->wm;
		  next_partial.wk=(m>>3)&p->wk;
		  q.bm=p->bm^next_partial.bm;
		  q.bk=p->bk^next_partial.bk;
   		  q.wm=p->wm^next_partial.wm;
   		  q.wk=p->wk^next_partial.wk;
      
		  whole_partial.bm=partial->bm^next_partial.bm;
		  whole_partial.bk=partial->bk^next_partial.bk;
		  whole_partial.wm=partial->wm^next_partial.wm;
		  whole_partial.wk=partial->wk^next_partial.wk;
		  BlackKingCapture2(&q,movelist,n,&whole_partial,m);
      
		  found=1;
	   }

	   /* right forward jump */
	   m=((((square&RFJ2)<<5)&white)<<4)&free;
	   if(m)
   	   {
		  next_partial.bm=0;
		  next_partial.bk=(m|(m>>9));
		  next_partial.wm=(m>>4)&p->wm;
		  next_partial.wk=(m>>4)&p->wk;
		  q.bm=p->bm^next_partial.bm;
		  q.bk=p->bk^next_partial.bk;
   		  q.wm=p->wm^next_partial.wm;
   		  q.wk=p->wk^next_partial.wk;
      
		  whole_partial.bm=partial->bm^next_partial.bm;
		  whole_partial.bk=partial->bk^next_partial.bk;
		  whole_partial.wm=partial->wm^next_partial.wm;
		  whole_partial.wk=partial->wk^next_partial.wk;
		  BlackKingCapture2(&q,movelist,n,&whole_partial,m);
      
		  found=1;
	   }

	   /* left backward jump */
	   m=((((square&LBJ2)>>4)&white)>>5)&free;
	   if(m)
   	   {
		  next_partial.bm=0;
		  next_partial.bk=(m|(m<<9));
		  next_partial.wm=(m<<5)&p->wm;
		  next_partial.wk=(m<<5)&p->wk;
		  q.bm=p->bm^next_partial.bm;
		  q.bk=p->bk^next_partial.bk;
   		  q.wm=p->wm^next_partial.wm;
   		  q.wk=p->wk^next_partial.wk;
      
		  whole_partial.bm=partial->bm^next_partial.bm;
		  whole_partial.bk=partial->bk^next_partial.bk;
		  whole_partial.wm=partial->wm^next_partial.wm;
		  whole_partial.wk=partial->wk^next_partial.wk;
		  BlackKingCapture2(&q,movelist,n,&whole_partial,m);
      
		  found=1;
		}

	   /* right backward jump */
	   m=((((square&RBJ2)>>3)&white)>>4)&free;
	   if(m)
   	   {
		  next_partial.bm=0;
		  next_partial.bk=(m|(m<<7));
		  next_partial.wm=(m<<4)&p->wm;
		  next_partial.wk=(m<<4)&p->wk;
		  q.bm=p->bm^next_partial.bm;
		  q.bk=p->bk^next_partial.bk;
   		  q.wm=p->wm^next_partial.wm;
   		  q.wk=p->wk^next_partial.wk;
      
		  whole_partial.bm=partial->bm^next_partial.bm;
		  whole_partial.bk=partial->bk^next_partial.bk;
		  whole_partial.wm=partial->wm^next_partial.wm;
		  whole_partial.wk=partial->wk^next_partial.wk;
		  BlackKingCapture2(&q,movelist,n,&whole_partial,m);
      
		  found=1;
	   }

	   if(!found)
   	   {
		  /* no continuing jumps - save the move in the movelist */          
		  movelist[*n]=*partial;
		  (*n)++;
	   }
   }

	void MakeWhiteKingCaptures(BitBoard* p, BitBoardMove* movelist, int& n)
	{
	   uint32 free,tmp,black;
	   BitBoardMove partial;
	   BitBoard q;
	   int m;
	   free=~(p->bm|p->bk|p->wm|p->wk);

	   if(p->wk)
       {
		    black=p->bm|p->bk;
      		/* jumps left forwards with black kings*/
			m=((((p->wk&LFJ1)<<3)&black)<<4)&free;
      		/* now m contains a bit for every free square where a black king can move*/
			 while(m)
   			 {
				tmp=(m&-m); /* least significant bit of m */
				partial.wm=0;
				partial.wk=(tmp|(tmp>>7));
				partial.bm=(tmp>>4)&p->bm;
				partial.bk=(tmp>>4)&p->bk;
				q.bm=p->bm^partial.bm;
   				q.bk=p->bk^partial.bk;
   				q.wm=p->wm^partial.wm;
   				q.wk=p->wk^partial.wk;
            
				WhiteKingCapture1(&q,movelist, &n, &partial,tmp);
            
      			m=m&(m-1);   /* clears least significant bit of m */
      		 }
			 m=((((p->wk&LFJ2)<<4)&black)<<3)&free;
      		/* now m contains a bit for every free square where a black king can move*/
			 while(m)
   			 {
				tmp=(m&-m); /* least significant bit of m */
				partial.wm=0;
				partial.wk=(tmp|(tmp>>7));
				partial.bm=(tmp>>3)&p->bm;
				partial.bk=(tmp>>3)&p->bk;
				q.bm=p->bm^partial.bm;
   				q.bk=p->bk^partial.bk;
   				q.wm=p->wm^partial.wm;
   				q.wk=p->wk^partial.wk;
            
				WhiteKingCapture2(&q,movelist,&n, &partial,tmp);
            
      			m=m&(m-1);   /* clears least significant bit of m */
      		}
			 /* jumps right forwards with black kings*/
			 m=((((p->wk&RFJ1)<<4)&black)<<5)&free;
      		/* now m contains a bit for every free square where a black king can move*/
			while(m)
   			{
				tmp=(m&-m); /* least significant bit of m */
				partial.wm=0;
				partial.wk=tmp|(tmp>>9);
				partial.bm=(tmp>>5)&p->bm;
				partial.bk=(tmp>>5)&p->bk;
				q.bm=p->bm^partial.bm;
   				q.bk=p->bk^partial.bk;
   				q.wm=p->wm^partial.wm;
   				q.wk=p->wk^partial.wk;
            
				WhiteKingCapture1(&q,movelist,&n, &partial,tmp);
            
      			m=m&(m-1);   /* clears least significant bit of m */
      		 }
			m=((((p->wk&RFJ2)<<5)&black)<<4)&free;
      		/* now m contains a bit for every free square where a black king can move*/
			while(m)
   			{
				tmp=(m&-m); /* least significant bit of m */
				partial.wm=0;
				partial.wk=(tmp|(tmp>>9));
				partial.bm=(tmp>>4)&p->bm;
				partial.bk=(tmp>>4)&p->bk;
				q.bm=p->bm^partial.bm;
   				q.bk=p->bk^partial.bk;
   				q.wm=p->wm^partial.wm;
   				q.wk=p->wk^partial.wk;
            
				WhiteKingCapture2(&q,movelist, &n, &partial,tmp);
            
      			m=m&(m-1);   /* clears least significant bit of m */
      		}

      		/* jumps left backwards with black kings*/
			m=((((p->wk&LBJ1)>>5)&black)>>4)&free;
      		/* now m contains a bit for every free square where a black king can move*/
			 while(m)
   			 {
				tmp=(m&-m); /* least significant bit of m */
				partial.wm=0;
				partial.wk=(tmp|(tmp<<9));
				partial.bm=(tmp<<4)&p->bm;
				partial.bk=(tmp<<4)&p->bk;
				q.bm=p->bm^partial.bm;
   				q.bk=p->bk^partial.bk;
   				q.wm=p->wm^partial.wm;
   				q.wk=p->wk^partial.wk;
            
				WhiteKingCapture1(&q,movelist,&n, &partial,tmp);
            
      			m=m&(m-1);   /* clears least significant bit of m */
      		}
			 m=((((p->wk&LBJ2)>>4)&black)>>5)&free;
      		/* now m contains a bit for every free square where a black king can move*/
			 while(m)
   			 {
				tmp=(m&-m); /* least significant bit of m */
				partial.wm=0;
				partial.wk=(tmp|(tmp<<9));
				partial.bm=(tmp<<5)&p->bm;
				partial.bk=(tmp<<5)&p->bk;
				q.bm=p->bm^partial.bm;
   				q.bk=p->bk^partial.bk;
   				q.wm=p->wm^partial.wm;
   				q.wk=p->wk^partial.wk;
            
				WhiteKingCapture2(&q,movelist, &n, &partial,tmp);
            
      			m=m&(m-1);   /* clears least significant bit of m */
      		 }
			 /* jumps right backwards with black kings*/
			 m=((((p->wk&RBJ1)>>4)&black)>>3)&free;
      		/* now m contains a bit for every free square where a black king can move*/
			 while(m)
   			 {
				tmp=(m&-m); /* least significant bit of m */
				partial.wm=0;
				partial.wk=tmp|(tmp<<7);
				partial.bm=(tmp<<3)&p->bm;
				partial.bk=(tmp<<3)&p->bk;
				q.bm=p->bm^partial.bm;
   				q.bk=p->bk^partial.bk;
   				q.wm=p->wm^partial.wm;
   				q.wk=p->wk^partial.wk;
            
				WhiteKingCapture1(&q,movelist,&n, &partial,tmp);
            
      			m=m&(m-1);   /* clears least significant bit of m */
      		 }
			 m=((((p->wk&RBJ2)>>3)&black)>>4)&free;
      		/* now m contains a bit for every free square where a black king can move*/
			 while(m)
   			 {
				tmp=(m&-m); /* least significant bit of m */
				partial.wm=0;
				partial.wk=(tmp|(tmp<<7));
				partial.bm=(tmp<<4)&p->bm;
				partial.bk=(tmp<<4)&p->bk;
            
   				q.bm=p->bm^partial.bm;
   				q.bk=p->bk^partial.bk;
   				q.wm=p->wm^partial.wm;
   				q.wk=p->wk^partial.wk;
				WhiteKingCapture2(&q,movelist,&n, &partial,tmp);
            
      			m=m&(m-1);   /* clears least significant bit of m */
      		 }
	    }	 
	}

	void MakeBlackKingCaptures(BitBoard* p, BitBoardMove* movelist, int& n)
	{
	   uint32 free,tmp,white;
	   BitBoardMove partial;
	   BitBoard q;
	   free=~(p->bm|p->bk|p->wm|p->wk);
	   int m;

	   if(p->bk)
       {
		    white=p->wm|p->wk;
      		/* jumps left forwards with white kings*/
			m=((((p->bk&LFJ1)<<3)&white)<<4)&free;
      		/* now m contains a bit for every free square where a white king can move*/
			while(m)
   			{
				tmp=(m&-m); /* least significant bit of m */
				partial.bm=0;
				partial.bk=(tmp|(tmp>>7));
				partial.wm=(tmp>>4)&p->wm;
				partial.wk=(tmp>>4)&p->wk;
				q.bm=p->bm^partial.bm;
   				q.bk=p->bk^partial.bk;
   				q.wm=p->wm^partial.wm;
   				q.wk=p->wk^partial.wk;
				BlackKingCapture1(&q,movelist, &n, &partial,tmp);
            
      			m=m&(m-1);   /* clears least significant bit of m */
      		}
			m=((((p->bk&LFJ2)<<4)&white)<<3)&free;
      		/* now m contains a bit for every free square where a white king can move*/
			while(m)
   			{
				tmp=(m&-m); /* least significant bit of m */
				partial.bm=0;
				partial.bk=(tmp|(tmp>>7));
				partial.wm=(tmp>>3)&p->wm;
				partial.wk=(tmp>>3)&p->wk;
				q.bm=p->bm^partial.bm;
   				q.bk=p->bk^partial.bk;
   				q.wm=p->wm^partial.wm;
   				q.wk=p->wk^partial.wk;
				BlackKingCapture2(&q,movelist, &n, &partial,tmp);
            
      			m=m&(m-1);   /* clears least significant bit of m */
      		}
			 /* jumps right forwards with white kings*/
			m=((((p->bk&RFJ1)<<4)&white)<<5)&free;
      		/* now m contains a bit for every free square where a white king can move*/
			while(m)
   			{
				tmp=(m&-m); /* least significant bit of m */
				partial.bm=0;
				partial.bk=tmp|(tmp>>9);
				partial.wm=(tmp>>5)&p->wm;
				partial.wk=(tmp>>5)&p->wk;
				q.bm=p->bm^partial.bm;
   				q.bk=p->bk^partial.bk;
   				q.wm=p->wm^partial.wm;
   				q.wk=p->wk^partial.wk;
				BlackKingCapture1(&q,movelist, &n, &partial,tmp);
            
      			m=m&(m-1);   /* clears least significant bit of m */
      		}
			m=((((p->bk&RFJ2)<<5)&white)<<4)&free;
      		/* now m contains a bit for every free square where a white king can move*/
			while(m)
   			{
				tmp=(m&-m); /* least significant bit of m */
				partial.bm=0;
				partial.bk=(tmp|(tmp>>9));
				partial.wm=(tmp>>4)&p->wm;
				partial.wk=(tmp>>4)&p->wk;
				q.bm=p->bm^partial.bm;
   				q.bk=p->bk^partial.bk;
   				q.wm=p->wm^partial.wm;
   				q.wk=p->wk^partial.wk;
				BlackKingCapture2(&q,movelist,&n, &partial,tmp);
            
      			m=m&(m-1);   /* clears least significant bit of m */
      		}
      		/* jumps left backwards with white kings*/
			m=((((p->bk&LBJ1)>>5)&white)>>4)&free;
      		/* now m contains a bit for every free square where a white king can move*/
			while(m)
   			{
				tmp=(m&-m); /* least significant bit of m */
				partial.bm=0;
				partial.bk=(tmp|(tmp<<9));
				partial.wm=(tmp<<4)&p->wm;
				partial.wk=(tmp<<4)&p->wk;
				q.bm=p->bm^partial.bm;
   				q.bk=p->bk^partial.bk;
   				q.wm=p->wm^partial.wm;
   				q.wk=p->wk^partial.wk;
				BlackKingCapture1(&q,movelist, &n, &partial,tmp);
            
      			m=m&(m-1);   /* clears least significant bit of m */
      		}
			m=((((p->bk&LBJ2)>>4)&white)>>5)&free;
      		/* now m contains a bit for every free square where a white king can move*/
			while(m)
   			{
				tmp=(m&-m); /* least significant bit of m */
				partial.bm=0;
				partial.bk=(tmp|(tmp<<9));
				partial.wm=(tmp<<5)&p->wm;
				partial.wk=(tmp<<5)&p->wk;
				q.bm=p->bm^partial.bm;
   				q.bk=p->bk^partial.bk;
   				q.wm=p->wm^partial.wm;
   				q.wk=p->wk^partial.wk;
				BlackKingCapture2(&q,movelist,&n, &partial,tmp);
            
      			m=m&(m-1);   /* clears least significant bit of m */
      		}
			 /* jumps right backwards with white kings*/
			m=((((p->bk&RBJ1)>>4)&white)>>3)&free;
      		/* now m contains a bit for every free square where a white king can move*/
			while(m)
   			{
				tmp=(m&-m); /* least significant bit of m */
				partial.bm=0;
				partial.bk=tmp|(tmp<<7);
				partial.wm=(tmp<<3)&p->wm;
				partial.wk=(tmp<<3)&p->wk;
				q.bm=p->bm^partial.bm;
   				q.bk=p->bk^partial.bk;
   				q.wm=p->wm^partial.wm;
   				q.wk=p->wk^partial.wk;
				BlackKingCapture1(&q,movelist,&n, &partial,tmp);
            
      			m=m&(m-1);   /* clears least significant bit of m */
      		}
			m=((((p->bk&RBJ2)>>3)&white)>>4)&free;
      		/* now m contains a bit for every free square where a white king can move*/
			while(m)
   			{
				tmp=(m&-m); /* least significant bit of m */
				partial.bm=0;
				partial.bk=(tmp|(tmp<<7));
				partial.wm=(tmp<<4)&p->wm;
				partial.wk=(tmp<<4)&p->wk;
				q.bm=p->bm^partial.bm;
   				q.bk=p->bk^partial.bk;
   				q.wm=p->wm^partial.wm;
   				q.wk=p->wk^partial.wk;
				BlackKingCapture2(&q,movelist, &n, &partial,tmp);
            
      			m=m&(m-1);   /* clears least significant bit of m */
      		}
	   } // if
	}

	int MakeCaptureList(BitBoard *bb, BitBoardMove* movelist, bool bWhite)
	{
		int index = 0;
		uint32 free=~(bb->bm|bb->bk|bb->wm|bb->wk);
   
		if(bWhite == true)
		{
			MakeWhiteKingCaptures(bb, movelist, index);
			MakeWhiteManCaptures(bb, movelist, index);     
		}
		else
		{
			MakeBlackKingCaptures(bb, movelist, index);
			MakeBlackManCaptures(bb, movelist, index);
		}

		return index;
	}


	 // Test Capture Methods
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////

	bool TestCapture(BitBoard *p, bool bWhite)
	{
		uint32 black,white,free,m;

		if (bWhite == true)
		{
			black=p->bm|p->bk;
			white=p->wm|p->wk;
			free=~(black|white);
		
			m =((((white&LFJ2)<<4)&black)<<3);
			m|=((((white&LFJ1)<<3)&black)<<4);
			m|=((((white&RFJ1)<<4)&black)<<5);
			m|=((((white&RFJ2)<<5)&black)<<4);
			if(p->wk)
			{
				m|=((((p->wk&LBJ1)>>5)&black)>>4);
				m|=((((p->wk&LBJ2)>>4)&black)>>5);
				m|=((((p->wk&RBJ1)>>4)&black)>>3);
				m|=((((p->wk&RBJ2)>>3)&black)>>4);
			}

			if(m & free)
				return true;
			return false;
		}
		else
		{
			black=p->bm|p->bk;
			white=p->wm|p->wk;
			free=~(black|white);

			m=((((black&LBJ1)>>5)&white)>>4);
			m|=((((black&LBJ2)>>4)&white)>>5);
			m|=((((black&RBJ1)>>4)&white)>>3);
			m|=((((black&RBJ2)>>3)&white)>>4);
			if(p->bk)
			{
				m|=((((p->bk&LFJ2)<<4)&white)<<3);
				m|=((((p->bk&LFJ1)<<3)&white)<<4);
				m|=((((p->bk&RFJ1)<<4)&white)<<5);
				m|=((((p->bk&RFJ2)<<5)&white)<<4);
			}

			if(m & free)
				return true;
			return false;
		}
	}

	int GenerateBitBoardMoves(BitBoard* bb, bool bWhite, BitBoardMove* movelist)
	{
		WATCH_START(T_BIT_GENERATOR);
		// number of moves
		int count = 0;

		if(TestCapture(bb, bWhite) == true)
		{
			count = MakeCaptureList(bb, movelist, bWhite);
		}
		else
		{
			count = MakeMoveList(bb, movelist, bWhite);
		}

		return count;
	}
}
